/*
 * @Date: 2022-07-27 14:27:15
 * @LastEditors: Zzt
 * @LastEditTime: 2022-08-08 18:23:39
 * @FilePath: \archives-fe\src\view\sysMaintenance\TaskCronE\contexts\TaskCronEContext.js
 */
import { createContext, useReducer, useEffect, useContext } from 'react';
import {
  ADDPLAN,
  DELETE_EXPORT_ITEM,
  SET_SELECTED_ITEM,
  AMEND_PLAN,
  VISIBLE,
  EDIT_PLAN,
  SEARCH
} from '../constants';
import { Modal } from 'antd';
// import { deleteAppAndDis } from '@/api/archives/export';
import { TaskCronEReducer } from '../reducers/TaskCronEReducer';
const initialState = {
  task: [
    { id: '1', taskname: '123', state: '开启', remark: '123', crone: '123' },
    { id: '100', taskname: '13' },
    { id: '3', taskname: '1235' }
  ],
  selectItems: [],
  selectId: [],
  choice: {},
  searchList: [],
  isVisible: false
};
const TaskCronEContext = createContext();
const TaskCronEContextProvider = (props) => {
  const [state, dispatch] = useReducer(TaskCronEReducer, initialState);
  const actions = {
    // 添加
    AddPlan(res) {
      console.log('context', res);
      const data = res;
      dispatch({ type: ADDPLAN, data });
    },
    // 需要删除的Id
    SelectId(selectArr) {
      console.log(selectArr);
      dispatch({ type: SET_SELECTED_ITEM, data: selectArr });
    },
    // 删除计划
    deleteExportFunc(id, title = '确认删除此计划？') {
      console.log('Fun', id);
      Modal.confirm({
        title: '删除',
        content: title,
        onOk() {
          const ids = id instanceof Array ? id.join(',') : id;
          console.log('ids', ids);
          // const data = ids.data.map((item) => parseInt(item));
          dispatch({ type: DELETE_EXPORT_ITEM, ids });
        },
        onCancel() {
          console.log('Cancel');
        }
      });
    },
    // 修改计划
    amend(plan) {
      console.log(plan);
      dispatch({ type: AMEND_PLAN, data: plan });
    },
    // 窗口可视
    setVisible(boll) {
      dispatch({ type: VISIBLE, data: boll });
    },
    // 修改的值
    EditPlan(choice) {
      console.log(choice);
      dispatch({ type: EDIT_PLAN, data: choice });
    },
    // 查询表达式
    setSearch(search) {
      dispatch({ type: SEARCH, data: search });
    }
  };
  useEffect(() => {}, []);
  return (
    <TaskCronEContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </TaskCronEContext.Provider>
  );
};
export const useTaskCronEContext = () => {
  return useContext(TaskCronEContext);
};
export { TaskCronEContext, TaskCronEContextProvider };
